//
//  SearchView.m
//  PigMarket
//
//  Created by mac on 2019/6/10.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "SearchView.h"

@interface SearchView ()<UISearchBarDelegate>

@end
@implementation SearchView

#pragma mark - init method
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initView];
        [self initLayout];
    }
    return self;
}

- (void)initView {
    // 添加子控件
    [self addSubview:self.searchBar];
}

- (void)initLayout {
    // 布局子控件
    [self.searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}
#pragma mark - delegate
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    
    if (self.searchBeginBlock) {
        self.searchBeginBlock();
        return NO;
    }
    return YES;
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
    [self.searchBar resignFirstResponder];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [self.searchBar resignFirstResponder];
    if (self.searchEndBlock) {
        self.searchEndBlock(searchBar.text);
    }
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    [self.searchBar resignFirstResponder];
}
#pragma mark - getter and setter
- (UISearchBar *)searchBar {
    if (!_searchBar) {
        _searchBar = [[UISearchBar alloc] init];
        
        //光标颜色
        //    self.searchBar.tintColor = [UIColor colorWithHexString:@"#F7F7F7"];
        
        //输入框背景色
        _searchBar.barTintColor = [UIColor whiteColor];
        
        //白色搜索框，灰色背景
        _searchBar.barStyle = UIBarStyleDefault;
        
        //显示取消按钮
        //_searchBar.showsCancelButton = YES;
        
        _searchBar.searchBarStyle = UISearchBarStyleDefault;
        
        //去掉自带黑线
        [self.searchBar setBackgroundImage:[UIImage new]];
        
        _searchBar.delegate = self;
        
        UITextField *searchField = [_searchBar valueForKey:@"_searchField"];
        //更改searchBar 中PlaceHolder 字体颜色
        [searchField setValue:[UIColor colorWithHexString:@"#B2B2B2"] forKeyPath:@"_placeholderLabel.textColor"];
        [searchField setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
    }
    return _searchBar;
}
//解决ios11  在titleview不显示问题
- (CGSize)intrinsicContentSize {
    return UILayoutFittingExpandedSize;
}
@end

